#!/usr/bin/env zsh

# Author: Hanley Lee
# Website: https://www.hanleylee.com
# GitHub: https://github.com/hanleylee
# License:  MIT License

# MARK: distinguish the output to give correct color code(e.g. no color for vim execute)
# generate leetcode file with order number
get_color() {
    if "$2"; then
        echo "$1"
    else
        echo ""
    fi
}

# MARK: success/info/error/warn {{{
msg() {
    printf '%b\n' "$1" >&2
}

success() {
    msg "${N_GREEN}[✔]${NC} ${1}${2}"
}

info() {
    msg "${N_BLUE}[➭]${NC} ${1}${2}"
}

warn () {
    msg "${N_YELLOW}[⚠]${NC} ${1}${2}"
}

error() {
    msg "${N_RED}[✘]${NC} ${1}${2}"
    exit 1
}
# }}}

# echo_with_color {{{
echo_with_color () {
    printf '%b\n' "$1$2${NC}" >&2
}
# }}}

# MARK: COLORS

# 是否应该有颜色
has_color=false
ncolors=$(tput colors)
if test -t 1 && test -n "$ncolors" && test "$ncolors" -ge 8; then
    has_color=true
fi
# export GREEN='\033[7;32;40m' # 绿底黑字
# export RED='\e[0;41;30m'     # 红底黑字

# terminal color template {{{
NC=$(get_color '\033[0m' "${has_color}")

# Regular Colors
N_BLACK=$(get_color '\033[0;30m' "${has_color}")  # Black
N_RED=$(get_color '\033[0;31m' "${has_color}")  # Red
N_GREEN=$(get_color '\033[0;32m' "${has_color}")  # Green
N_YELLOW=$(get_color '\033[0;33m' "${has_color}")  # Yellow
N_BLUE=$(get_color '\033[0;34m' "${has_color}")  # Blue
N_PURPLE=$(get_color '\033[0;35m' "${has_color}")  # Purple
N_CYAN=$(get_color '\033[0;36m' "${has_color}")  # Cyan
N_WHITE=$(get_color '\033[0;37m' "${has_color}")  # White

# Bold
B_BLACK=$(get_color '\033[1;30m' "${has_color}")  # Black
B_RED=$(get_color '\033[1;31m' "${has_color}")  # Red
B_GREEN=$(get_color '\033[1;32m' "${has_color}")  # Green
B_YELLOW=$(get_color '\033[1;33m' "${has_color}")  # Yellow
B_BLUE=$(get_color '\033[1;34m' "${has_color}")  # Blue
B_PURPLE=$(get_color '\033[1;35m' "${has_color}")  # Purple
B_CYAN=$(get_color '\033[1;36m' "${has_color}")  # Cyan
B_WHITE=$(get_color '\033[1;37m' "${has_color}")  # White

# Underline
U_BLACK=$(get_color '\033[4;30m' "${has_color}")  # Black
U_RED=$(get_color '\033[4;31m' "${has_color}")  # Red
U_GREEN=$(get_color '\033[4;32m' "${has_color}")  # Green
U_YELLOW=$(get_color '\033[4;33m' "${has_color}")  # Yellow
U_BLUE=$(get_color '\033[4;34m' "${has_color}")  # Blue
U_PURPLE=$(get_color '\033[4;35m' "${has_color}")  # Purple
U_CYAN=$(get_color '\033[4;36m' "${has_color}")  # Cyan
U_WHITE=$(get_color '\033[4;37m' "${has_color}")  # White

# High Intensity
I_BLACK=$(get_color '\033[0;90m' "${has_color}")  # Black
I_RED=$(get_color '\033[0;91m' "${has_color}")  # Red
I_GREEN=$(get_color '\033[0;92m' "${has_color}")  # Green
I_YELLOW=$(get_color '\033[0;93m' "${has_color}")  # Yellow
I_BLUE=$(get_color '\033[0;94m' "${has_color}")  # Blue
I_PURPLE=$(get_color '\033[0;95m' "${has_color}")  # Purple
I_CYAN=$(get_color '\033[0;96m' "${has_color}")  # Cyan
I_WHITE=$(get_color '\033[0;97m' "${has_color}")  # White

# Bold High Intensity
B_I_BLACK=$(get_color '\033[1;90m' "${has_color}")  # Black
B_I_RED=$(get_color '\033[1;91m' "${has_color}")  # Red
B_I_GREEN=$(get_color '\033[1;92m' "${has_color}")  # Green
B_I_YELLOW=$(get_color '\033[1;93m' "${has_color}")  # Yellow
B_I_BLUE=$(get_color '\033[1;94m' "${has_color}")  # Blue
B_I_PURPLE=$(get_color '\033[1;95m' "${has_color}")  # Purple
B_I_CYAN=$(get_color '\033[1;96m' "${has_color}")  # Cyan
B_I_WHITE=$(get_color '\033[1;97m' "${has_color}")  # White

# Background
BG_BLACK=$(get_color '\033[40m' "${has_color}")    # Black
BG_RED=$(get_color '\033[41m' "${has_color}")    # Red
BG_GREEN=$(get_color '\033[42m' "${has_color}")    # Green
BG_YELLOW=$(get_color '\033[43m' "${has_color}")    # Yellow
BG_BLUE=$(get_color '\033[44m' "${has_color}")    # Blue
BG_PURPLE=$(get_color '\033[45m' "${has_color}")    # Purple
BG_CYAN=$(get_color '\033[46m' "${has_color}")    # Cyan
BG_WHITE=$(get_color '\033[47m' "${has_color}")    # White

# High Intensity backgrounds
BG_I_BLACK=$(get_color '\033[0;100m' "${has_color}") # Black
BG_I_RED=$(get_color '\033[0;101m' "${has_color}") # Red
BG_I_GREEN=$(get_color '\033[0;102m' "${has_color}") # Green
BG_I_YELLOW=$(get_color '\033[0;103m' "${has_color}") # Yellow
BG_I_BLUE=$(get_color '\033[0;104m' "${has_color}") # Blue
BG_I_PURPLE=$(get_color '\033[0;105m' "${has_color}") # Purple
BG_I_CYAN=$(get_color '\033[0;106m' "${has_color}") # Cyan
BG_I_WHITE=$(get_color '\033[0;107m' "${has_color}") # White
# }}}
